package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.platform.entity.apply.OrgApplyJinRuiXinXi;
import com.ts.api.http.platform.entity.dataexchange.OrgDataExchangeJinRuiXinXi;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 * 东莞市金瑞信息
 * @author 李源聪
 */
@ApiPlan(productId = "29", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JinRuiXinXi implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "bYCL52dyLbZH232V";
    private static final String IV = "DGcv9U76f22Ufkd3";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {

        OrgDataExchangeJinRuiXinXi orgDataExchangeJinRuiXinXi = new OrgDataExchangeJinRuiXinXi();
        orgDataExchangeJinRuiXinXi.setMd5_phone(apiDataExchangeParameter.getPhoneMd5());

        return JsonUtils.toJson(orgDataExchangeJinRuiXinXi);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyJinRuiXinXi orgApplyJinRuiXinXi = new OrgApplyJinRuiXinXi();
        orgApplyJinRuiXinXi.setName(apiApplyParameter.getUserName());
        String phone = apiApplyParameter.getPhone();
        orgApplyJinRuiXinXi.setMobile(phone);

        String loanAmountChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", "30000")
                .when(apiApplyParameter.getLoanAmount(), "2", "50000")
                .when(apiApplyParameter.getLoanAmount(), "3", "100000")
                .when(apiApplyParameter.getLoanAmount(), "4", "200000")
                .end();
        orgApplyJinRuiXinXi.setMain_bcwumx(loanAmountChoose);

        String loanPurposeChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", "旅游")
                .when(apiApplyParameter.getLoanPurpose(), "2", "教育")
                .when(apiApplyParameter.getLoanPurpose(), "3", "婚庆")
                .when(apiApplyParameter.getLoanPurpose(), "4", "装修")
                .when(apiApplyParameter.getLoanPurpose(), "5", "医疗")
                .when(apiApplyParameter.getLoanPurpose(), "6", "个人消费")
                .end();
        orgApplyJinRuiXinXi.setMain_pphvfj(loanPurposeChoose);

        String city = apiApplyParameter.getCity();
        orgApplyJinRuiXinXi.setMain_brmisw(city);

        String careerChoose = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "上班族")
                .when(apiApplyParameter.getCareer(), "2", "自由职业")
                .when(apiApplyParameter.getCareer(), "3", "企业主（有营业执照）")
                .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                .when(apiApplyParameter.getCareer(), "5", "个体户")
                .end();
        orgApplyJinRuiXinXi.setMain_exjrcp(careerChoose);

        String age = apiApplyParameter.getAge();
        orgApplyJinRuiXinXi.setMain_yvzrib(age);

        String monthlySalaryChoose = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", "3000")
                .when(apiApplyParameter.getMonthlySalary(), "2", "8000")
                .when(apiApplyParameter.getMonthlySalary(), "3", "15000")
                .when(apiApplyParameter.getMonthlySalary(), "4", "30000")
                .when(apiApplyParameter.getMonthlySalary(), "5", "50000")
                .end();
        orgApplyJinRuiXinXi.setMain_rqtujb(monthlySalaryChoose);

        //性别  1男2女0未知
        Integer sexChoose = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .when(apiApplyParameter.getSex(), "2", 0)
                .end();
        orgApplyJinRuiXinXi.setMain_btfetq(sexChoose);

        String educationAttainmentChoose = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", "初中及以下")
                .when(apiApplyParameter.getEducationAttainment(), "2", "中专")
                .when(apiApplyParameter.getEducationAttainment(), "3", "高中")
                .when(apiApplyParameter.getEducationAttainment(), "4", "大专")
                .when(apiApplyParameter.getEducationAttainment(), "5", "本科")
                .when(apiApplyParameter.getEducationAttainment(), "6", "研究生及以上")
                .end();
        orgApplyJinRuiXinXi.setMain_rfypdr(educationAttainmentChoose);

        //房产 1有房产 2无
        Integer realEstateInfoChoose = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(2);
        orgApplyJinRuiXinXi.setMain_xktbum(realEstateInfoChoose);

        //车产 1有车产 2 无
        Integer carInfoChoose = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 2)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end();
        orgApplyJinRuiXinXi.setMain_qxjooz(carInfoChoose);

        //芝麻分 1（600-650）2（650-700）3（700以上）4（600以下）5（无）
        Integer sesameCreditChoose = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 4)
                .when(apiApplyParameter.getSesameCredit(), "2", 1)
                .when(apiApplyParameter.getSesameCredit(), "3", 2)
                .when(apiApplyParameter.getSesameCredit(), "4", 3)
                .end();
        orgApplyJinRuiXinXi.setMain_cssuwz(sesameCreditChoose);

        //微粒贷 1（5000）2（10000） 3（20000）4(30000)  5(50000）6（50000以上)  7(无)
        orgApplyJinRuiXinXi.setMain_goydtj(7);

        //公积金1（有公积金）2（6个月以下）3（6个月以上）4（无）
        Integer providentFundChoose = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 4)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 3)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end();
        orgApplyJinRuiXinXi.setMain_lgtqna(providentFundChoose);

        //社保1（有社保）2（6个月以下）3（6个月以上）4（无）
        Integer socialSecurityChoose = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 4)
                .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                .when(apiApplyParameter.getSocialSecurity(), "3", 3)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end();
        orgApplyJinRuiXinXi.setMain_fdbrrx(socialSecurityChoose);

        //保单1（有保单）2（6个月以下）3（6个月以上）4（无）
        Integer insurancePolicyChoose = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 4)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 3)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end();
        orgApplyJinRuiXinXi.setMain_zvqzwn(insurancePolicyChoose);

        //信用卡 1有  2无
        Integer creditLimitChoose = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 2)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 1)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end();
        orgApplyJinRuiXinXi.setMain_wrgjyk(creditLimitChoose);

        //营业执照 1有营业执照 2无
        Integer bizLicenseYearChoose = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 2)
                .end();
        orgApplyJinRuiXinXi.setMain_uimmgv(bizLicenseYearChoose);

        //逾期状态 1有逾期  2无
        Integer overdueChoose = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 1)
                .when(apiApplyParameter.getOverdue(), "2", 2)
                .end();
        orgApplyJinRuiXinXi.setMain_ebyush(overdueChoose);

        String hbLimitChoose = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", "0")
                .when(apiApplyParameter.getHbLimit(), "2", "1000")
                .when(apiApplyParameter.getHbLimit(), "3", "5000")
                .when(apiApplyParameter.getHbLimit(), "4", "10000")
                .when(apiApplyParameter.getHbLimit(), "5", "30000")
                .end();
        orgApplyJinRuiXinXi.setMain_nohfxu(hbLimitChoose);

        String btLimitChoose = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", "0")
                .when(apiApplyParameter.getBtLimit(), "2", "1000")
                .when(apiApplyParameter.getBtLimit(), "3", "5000")
                .when(apiApplyParameter.getBtLimit(), "4", "10000")
                .when(apiApplyParameter.getBtLimit(), "5", "30000")
                .end();
        orgApplyJinRuiXinXi.setMain_gikyzj(btLimitChoose);

        orgApplyJinRuiXinXi.setMain_dqofuk(apiApplyParameter.getFormRemark());

        orgApplyJinRuiXinXi.setSource("安心分期");

        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(orgApplyJinRuiXinXi));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> newMap.put("content", dataStr));
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
